package org.oschina.api.user.user.queue;

import org.oschina.Constants;
import org.oschina.api.user.user.dao.UserDAO;
import org.oschina.api.user.user.model.User;
import org.oschina.plugin.PluginHelper;
import org.oschina.plugin.PluginType;
import org.oschina.queue.BaseQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 用户下线处理队列
 */
@Component
public class UserLogoutHandleQueue extends BaseQueue {

	private final static Logger logger=LoggerFactory.getLogger(UserLogoutHandleQueue.class);
	
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private PluginHelper pluginHelper;
	
	@Scheduled(fixedDelay=1000)
	private synchronized void handle()
	{
		User user=(User) get(Constants.QUEUE_USER_LOGOUT_HANDLE,User.class);
		if(user==null)
			return;
		
		userDAO.removeCache(user.getId());
		userDAO.removeOnlineCache(user.getId());
		pluginHelper.execute(PluginType.LOGOUT, user);
		logger.info("ID为 '{}',用户名为'{}' - 的用户数据已处理.",user.getId(),user.getUsername());
	}
}